package com.caimiao.miao.library.usb.logic;

import android.util.Log;

import com.caimiao.miao.library.usb.interfaces.IUsbReadCallback;
import com.caimiao.miao.library.usb.accessory.Command;

import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

public class TicketOnSubscribe implements ObservableOnSubscribe<Command> {
    private int maxCount;

    public TicketOnSubscribe(int maxCount) {
        this.maxCount = maxCount;
    }

    @Override
    public void subscribe(final ObservableEmitter<Command> emitter) throws Exception {
        IUsbReadCallback callback = new IUsbReadCallback() {
            @Override
            public void call(Command command) {
                Log.e("TicketOnSubscribe", "command=" + command);
                maxCount--;
                if (maxCount <= 0)
                    emitter.onComplete();
                else
                    emitter.onNext(command);
            }
        };
        UsbUtil.getInstance().addUsbReadCallback(callback);
    }
}
